//
//  VideoDetailCell.m
//  Tang
//
//  Created by lanou on 15/12/4.
//  Copyright © 2015年 352324497@qq.com. All rights reserved.
//

#import "VideoDetailCell.h"

#define cellwidth (KWidth - 2 * KWidth * 10 / 414)
#define contenHei (21 *KWidth / 414)
#define Kspace (5*KWidth / 414)

@interface VideoDetailCell ()
@property (nonatomic, strong) UIImageView *myIcon;//图片
@property (nonatomic, strong) UIImageView *nameIcon;
@property (nonatomic, strong) UILabel *titleLabel;//标题
@property (nonatomic, strong) UILabel *nameLabel;//名字
//@property (nonatomic, strong) UILabel *subCount;//订阅人数
@property (nonatomic, strong) UILabel *seeLabel;//看过人数;
@property (nonatomic, strong) UIImageView *playIcon;//播放
@property (nonatomic, strong) UILabel *duLabel;//播放时间
@property (nonatomic, strong) UILabel *lineLabel;//下方横线

@end
@implementation VideoDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = NO;
        self.myIcon = [[UIImageView alloc] init];
        self.nameIcon = [[UIImageView alloc] init];
        
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:contenHei];
        self.titleLabel.alpha = 0.8;
        self.titleLabel.numberOfLines = 0;
        //self.titleLabel.textColor = [UIColor blackColor];
        
        
        self.nameLabel = [[UILabel alloc] init];
        self.nameLabel.alpha = 0.6;
        self.nameLabel.font = [UIFont systemFontOfSize:contenHei / 1.5];
        
       
        self.seeLabel = [[UILabel alloc] init];
        self.seeLabel.font = [UIFont systemFontOfSize:contenHei / 1.5];
        self.seeLabel.alpha = 0.6;
        self.seeLabel.textAlignment = NSTextAlignmentRight;
        self.playIcon = [[UIImageView alloc] init];
        
        self.duLabel = [[UILabel alloc] init];
        self.duLabel.font =[UIFont systemFontOfSize:contenHei / 1.5];
        self.duLabel.backgroundColor = [UIColor grayColor];
        self.duLabel.textAlignment = NSTextAlignmentCenter;
        self.duLabel.textColor = [UIColor whiteColor];
        
        self.lineLabel = [[UILabel alloc] init];
        self.lineLabel.backgroundColor = [UIColor grayColor];
        self.lineLabel.alpha = 0.6;
        
        self.like = [[DIYButton alloc]init];
        self.like.iconImageView.image = [UIImage imageNamed:@"like"];
        self.like.selectIconImageView.image = [UIImage imageNamed:@"islike"];
        [self.contentView addSubview:self.like];

        
        [self.contentView addSubview:_myIcon];
        [self.contentView addSubview:_nameIcon];
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_nameLabel ];
        [self.contentView addSubview:_seeLabel];
        [self.contentView addSubview:_playIcon];
        [self.contentView addSubview:_duLabel];
        [self.contentView addSubview:_lineLabel];
        
    }
    return  self;
}

// 跟新frame
- (void)upFrameWithModel
{
    CGFloat x = (KWidth - cellwidth)/2;
    CGFloat LbHeight = 25 * KWidth / 414;
    NSInteger iconWi = [_model.course_cover_width integerValue];
    NSInteger iconHe = [_model.course_cover_height integerValue];
    CGFloat iconHeight = KWidth / iconWi *iconHe;
    
    self.myIcon.frame = CGRectMake(0, 0, KWidth, iconHeight);
    self.titleLabel.frame = CGRectMake(x, iconHeight +Kspace, cellwidth, [self cellHeightFromModel]);
    CGFloat y = iconHeight + self.titleLabel.height + Kspace*2;
    
    
    self.nameIcon.frame = CGRectMake(x, y, LbHeight, LbHeight);
    [self.nameIcon.layer setCornerRadius:LbHeight / 2];
    self.nameIcon.layer.masksToBounds = YES;
    
    
    
    self.nameLabel.frame = CGRectMake(x + LbHeight +10, y, cellwidth / 3, LbHeight);
    CGFloat seeWi = cellwidth / 3;
    self.seeLabel.frame = CGRectMake(cellwidth + x - seeWi, y, seeWi, LbHeight);
    self.like.frame = CGRectMake((KWidth - cellwidth / 3)/1.2, y, cellwidth / 3, LbHeight*1.3);
    if ([[CoreDataMan shareIntance]findPlayWithId:_model.course_video])
    {
        self.like.selected = YES;
    }
    else
    {
        self.like.selected = NO;
    }

    
    self.playIcon.frame = CGRectMake(0, 0, LbHeight * 2, LbHeight * 2);
    self.playIcon.center = self.myIcon.center;
    
    
    self.duLabel.frame = CGRectMake(self.myIcon.center.x * 1.5, self.myIcon.center.y / 5, cellwidth / 6, LbHeight);
    [self.duLabel.layer setCornerRadius:LbHeight / 2];
    self.duLabel.layer.masksToBounds = YES;
    
    
    y = y + self.seeLabel.height + Kspace;
    self.lineLabel.frame = CGRectMake(0, y, KWidth, 3 * KWidth / 414);
    
    
    
    
    
    
}

- (CGFloat)cellHeightFromModel
{
    CGRect bounds = [_model.course_title boundingRectWithSize:CGSizeMake(cellwidth, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:contenHei]} context:nil ];
    return bounds.size.height;
}

+ (CGFloat)cellHeightFromModel:(VideoDetailModel *)model
{
    
    NSInteger iconWi = [model.course_cover_width integerValue];
    NSInteger iconHe = [model.course_cover_height integerValue];
    CGFloat iconHeight = KWidth / iconWi *iconHe;

    CGRect bounds = [model.course_title boundingRectWithSize:CGSizeMake(cellwidth, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:contenHei]} context:nil ];
    
    
    return iconHeight + bounds.size.height + Kspace * 2+ 25 * KWidth / 414 + Kspace + 3 * KWidth / 414 ;
}
- (void)setModel:(VideoDetailModel *)model
{
    _model = model;
    [self.myIcon sd_setImageWithURL:[NSURL URLWithString:model.course_cover] placeholderImage:[UIImage imageNamed:@"PCe"]];
    self.titleLabel.text = model.course_title;
    [self.nameIcon sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"happy"]];
    self.nameLabel.text = model.nickname;
    self.seeLabel.text = [NSString stringWithFormat:@"%@人看过",model.see_count];
    
    self.playIcon.image = [UIImage imageNamed:@"play"];
    self.duLabel.text = model.video_time;
    
    [self upFrameWithModel];
    
    
}

@end
